/**
* @file queue_processor.h
*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef QUEUE_PROCESSOR_H
#define QUEUE_PROCESSOR_H

#include <mutex>
#include <memory>
#include <map>
#include "queue.h"
#include "mmpa/mmpa_api.h"

namespace acl {

using QueueDataMutex = struct TagQueueDataMutex {
    std::mutex muForEnqueue;
    std::mutex muForDequeue;
};

enum PID_QUERY_TYPE : int32_t {
    CP_PID,
    QS_PID
};

constexpr int32_t MSEC_TO_USEC = 1000;

using QueueDataMutexPtr = std::shared_ptr<QueueDataMutex>;

class QueueProcessor
{
public:
    virtual aclError acltdtCreateQueue(const acltdtQueueAttr *const attr, uint32_t *const qid) = 0;

    virtual aclError acltdtDestroyQueue(const uint32_t qid) = 0;

    virtual aclError acltdtEnqueue(const uint32_t qid, const acltdtBuf buf, const int32_t timeout);

    virtual aclError acltdtDequeue(const uint32_t qid, acltdtBuf *const buf, const int32_t timeout);

    virtual aclError acltdtGrantQueue(const uint32_t qid, const int32_t pid, const uint32_t permission,
        const int32_t timeout);

    virtual aclError acltdtAttachQueue(const uint32_t qid, const int32_t timeout,
        uint32_t *const permission);

    virtual aclError acltdtBindQueueRoutes(acltdtQueueRouteList *const qRouteList) = 0;

    virtual aclError acltdtUnbindQueueRoutes(acltdtQueueRouteList *const qRouteList) = 0;

    virtual aclError acltdtQueryQueueRoutes(const acltdtQueueRouteQueryInfo *const queryInfo,
                                            acltdtQueueRouteList *const qRouteList) = 0;

    virtual aclError acltdtAllocBuf(const size_t size, acltdtBuf *const buf) const;

    virtual aclError acltdtFreeBuf(const acltdtBuf buf) const;

    virtual aclError acltdtGetBufData(const acltdtBuf buf, void **const dataPtr, size_t *const size);

    aclError InitQueueSchedule(const int32_t devId) const;

    aclError acltdtDestroyQueueOndevice(const uint32_t qid, const bool isThreadMode = false);

    aclError SendBindUnbindMsgOnDevice(acltdtQueueRouteList *const qRouteList,
        const bool isBind, rtEschedEventSummary_t &eventSum, rtEschedEventReply_t &ack) const;

    aclError SendConnectQsMsg(const int32_t deviceId, rtEschedEventSummary_t &eventSum, rtEschedEventReply_t &ack);
    aclError GetDstInfo(const int32_t deviceId, const PID_QUERY_TYPE type,
        int32_t &dstPid, const bool isThreadMode = false) const;
    aclError GetQueuePermission(const int32_t deviceId, uint32_t qid, rtMemQueueShareAttr_t &permission) const;
    aclError GetQueueRouteNum(const acltdtQueueRouteQueryInfo *const queryInfo,
                              const int32_t deviceId,
                              rtEschedEventSummary_t &eventSum,
                              rtEschedEventReply_t &ack,
                              size_t &routeNum) const;

    aclError QueryQueueRoutesOnDevice(const acltdtQueueRouteQueryInfo *const queryInfo, const size_t routeNum,
        rtEschedEventSummary_t &eventSum, rtEschedEventReply_t &ack, acltdtQueueRouteList *const qRouteList) const;

    QueueDataMutexPtr GetMutexForData(const uint32_t qid);
    void DeleteMutexForData(const uint32_t qid);

    uint64_t GetTimestamp() const;

    aclError GetDeviceId(int32_t& deviceId) const;


    virtual aclError acltdtEnqueueData(const uint32_t qid, const void *const data, const size_t dataSize,
        const void *const userData, const size_t userDataSize, const int32_t timeout, const uint32_t rsv);


    aclError acltdtDequeueData(const uint32_t qid, void *const data, const size_t dataSize, size_t *const retDataSize,
        void *const userData, const size_t userDataSize, const int32_t timeout);

    // set queue attr to default,depth is 8,name is empty
    static void acltdtSetDefaultQueueAttr(acltdtQueueAttr &attr);

    aclError acltdtCreateQueueWithAttr(const int32_t deviceId, const acltdtQueueAttr *const attr,
        uint32_t *const qid) const;

    QueueProcessor() = default;
    virtual ~QueueProcessor() = default;

    // not allow copy constructor and assignment operators
    QueueProcessor(const QueueProcessor &) = delete;

    QueueProcessor &operator=(const QueueProcessor &) = delete;

    QueueProcessor(QueueProcessor &&) = delete;

    QueueProcessor &&operator=(QueueProcessor &&) = delete;

protected:
    std::recursive_mutex muForQueueCtrl_;
    std::mutex muForQueueMap_;
    std::map<uint32_t, QueueDataMutexPtr> muForQueue_;
    bool isQsInit_ = false;
    uint32_t qsContactId_ = 0U;
    static bool isInitQs_;
};
}



#endif // QUEUE_PROCESS_H